﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Facebook;
using RoccaStore.Services;
using RoccaStore.Web.Filters;

namespace RoccaStore.Web.Controllers
{
    [AccountFilter]
    public class AccountController : Controller
    {
        private string Scope = "user_about_me,publish_stream,user_location,email";

        private readonly IAccountService _accountService;

        public AccountController(
            IAccountService accountService)
        {
            _accountService = accountService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logout()
        {
            _accountService.Logout();
            return Redirect("~/");
        }

        [AllowAnonymous]
        public ActionResult FacebookLogin()
        {
            var fb = new FacebookClient();
            var fbLoginUrl = fb.GetLoginUrl(
                new
                {
                    client_id = Config.AppId,
                    client_secret = Config.AppSecret,
                    redirect_uri = "http://" + Request.Url.Authority + "/account/facebookcallback/",
                    response_type = "code",
                    scope = Scope
                });

            return Redirect(fbLoginUrl.AbsoluteUri);
        }

        [AllowAnonymous]
        public ActionResult FacebookCallback(string code)
        {
            var fb = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id = Config.AppId,
                client_secret = Config.AppSecret,
                redirect_uri = "http://" + Request.Url.Authority + "/account/facebookcallback/",
                code = code
            });

            var accessToken = result.access_token;
            
            var client = new FacebookClient(accessToken);
            dynamic me = client.Get("me");                        

            string aboutMe = me.about;
            string id = me.id;

            Account account = _accountService.GetByFacebookID(id);

            if(account == null)
            {
                string email = me.email;
                string name = me.name;
                string firstname = me.first_name;
                string lastname = me.last_name;
                string location = me.location.name;

                dynamic post = client.Post("me/feed", new { message = "Send and share large files with ease using Rocca. Store", link = "http://store.rocca.cc/", picture = "http://store.rocca.cc/content/images/logo.png" });

                account = new Account()
                {
                    FacebookUserID = id,
                    FacebookToken = accessToken,
                    FacebookData = Newtonsoft.Json.JsonConvert.SerializeObject(me),
                    DisplayName = name,
                    FirstName = firstname,
                    LastName = lastname,
                    Location = location,
                    Email = email
                };

                _accountService.Save(account);
            }

            _accountService.Authorize(account);

            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }
    }
}
